package net.simo.servicebook.controller;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import net.simo.servicebook.controller.login.Login;
import net.simo.servicebook.model.Color;
import net.simo.servicebook.model.Maintenance;
import net.simo.servicebook.model.Person;
import net.simo.servicebook.model.Vehicle;
import net.simo.servicebook.model.VehicleMake;
import net.simo.servicebook.model.VehicleModel;
import net.simo.servicebook.model.VehicleModification;
import net.simo.servicebook.service.VehicleManager;

@Named
@ConversationScoped
public class VehicleController implements Serializable {

	private static final long serialVersionUID = -8788524513629076821L;

	@Inject
	private VehicleManager manager;
	@Inject
	private Login login;
	private List<Vehicle> vehicles;
	private Vehicle vehicle;
	private Vehicle newVehicle;
	private Maintenance newMaintenance;
	private List<Maintenance> maintenances;
	@Inject
	private Conversation conversation;
	private List<VehicleMake> makes;
	private List<VehicleModel> models;
	private List<VehicleModification> modifications;
	private VehicleMake currentMake;
	private VehicleModel currentModel;
	private VehicleModification currentModification;
	private List<Color> currentColors;

	public List<Color> getCurrentColors() {
		return currentColors;
	}

	public VehicleModification getCurrentModification() {
		return currentModification;
	}

	public void setCurrentModification(VehicleModification currentModification) {
		this.currentModification = currentModification;
	}

	public VehicleModel getCurrentModel() {
		return currentModel;
	}

	public void setCurrentModel(VehicleModel currentModel) {
		this.currentModel = currentModel;
	}

	@Produces
	public Vehicle getNewVehicle() {
		return newVehicle;
	}

	public VehicleMake getCurrentMake() {
		return currentMake;
	}

	@Produces
	public List<VehicleMake> getMakes() {
		return makes;
	}

	@Produces
	public List<VehicleModel> getModels() {
		return models;
	}

	public List<VehicleModification> getModifications() {
		return modifications;
	}

	@Produces
	@Named
	public Maintenance getNewMaintenance() {
		return newMaintenance;
	}

	public List<Maintenance> getMaintenances() {
		return maintenances;
	}

	public void setMaintenances(List<Maintenance> maintenances) {
		this.maintenances = maintenances;
	}

	public Vehicle getVehicle() {
		return vehicle;
	}

	public void setVehicle(Vehicle vehicle) {
		this.vehicle = vehicle;
	}

	public List<Vehicle> getVehicles() {
		return vehicles;
	}

	public void setVehicles(List<Vehicle> vehicles) {
		this.vehicles = vehicles;
	}

	public void selectVehicle(Vehicle vehicle) throws IOException {
		this.vehicle = vehicle;
		this.maintenances = manager.getMaintenance(vehicle);
		String redirectUrl = "/maintenances.jsf";
		FacesContext.getCurrentInstance().getExternalContext().redirect(redirectUrl);
	}

	public void createVehicle(Person person) {
		this.vehicles = manager.personVehicle(person);
		makes = manager.getMakes();
		newVehicle = new Vehicle();
		newVehicle.setOwner(person);
		newVehicle.setColor(new Color());

		currentMake = new VehicleMake();
		currentModel = new VehicleModel();
		currentModification = new VehicleModification();

		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/adminVehicle.jsf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<VehicleModel> getCurrentModels() {
		if (currentMake == null) {
			return Collections.emptyList();
		}
		models = manager.getModels(currentMake);
		return models;
	}

	public List<VehicleModification> getCurrentModifications() {
		if (currentModel == null) {
			return Collections.emptyList();
		}
		modifications = manager.getModifications(currentModel);
		return modifications;
	}

	public List<Color> getColors() {
		currentColors = manager.getColors();
		return currentColors;
	}

	public void saveVehicle() {
		newVehicle.setVehicleModel(currentModel);
		newVehicle.setVehicleModification(currentModification);
		manager.saveVehicle(newVehicle);
		newVehicle = null;
		conversation.end();
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("registration.jsf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void newMaintenance() {
		newMaintenance = new Maintenance();
		this.newMaintenance.setVehicle(vehicle);
	}

	public void editMaintenance(Maintenance maintenance) {
		this.newMaintenance = maintenance;
	}

	public void saveMaintenance() {
		manager.saveMaintenance(newMaintenance);
		newMaintenance = null;
		this.maintenances = manager.getMaintenance(vehicle);
	}

	public void deleteMaintenance(Maintenance maintenance) {
		maintenances.remove(maintenance);
		manager.deleteMaintenance(maintenance);
	}

	public void editMaintenance() {
		manager.editMaintenance(newMaintenance);
		newMaintenance = null;
	}

	public void cancelMaintenance() {
		newMaintenance = null;
	}

	@PostConstruct
	public void init() {
		if (conversation.isTransient()) {
			conversation.begin();
		}
		vehicles = manager.userVehicle(login.getCurrentUser());
	}
}
